package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : ZhangHao
 * @version :1.0
 * @date :Created in 2020/1/2
 * @description : 菜单控制器
 */
@RequestMapping("/menu")
@RestController
@Slf4j
public class MenuController {
    @Reference
    private MenuService menuService;

    /**
     * 根据权限动态展示菜单
     *
     * @return
     */
    @GetMapping("showMenu")
    public Result showMenu() {
        //获取当前用户
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (null != principal && principal instanceof org.springframework.security.core.userdetails.User) {
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) principal;
            String username = user.getUsername();
            List<Menu> menuList = menuService.showMenuByUserName(username);
            log.info("[动态显示菜单]: {}==>可以访问的页面:{}", username, menuList);
            return new Result(true, MessageConst.GET_MENU_SUCCESS, menuList);
        }
        return new Result(false, MessageConst.GET_USERNAME_NULL);
    }

    /**
     * 分页查询菜单
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[菜单分页]  queryPageBean:{}", queryPageBean);
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        return new Result(true, MessageConst.GET_MENU_SUCCESS, pageResult);
    }

    @PostMapping("add")
    public Result add(@RequestBody Menu menu) {
        log.info("[添加菜单] menu:{}", menu);
        menuService.add(menu);
        return new Result(true, MessageConst.ADD_MENU_SUCCESS);
    }

    @GetMapping("findById")
    public Result findById(Integer id) {
        Menu menu = menuService.findById(id);
        log.info("[编辑菜单] 正在编辑的menu是 : {}", menu);
        return new Result(true, MessageConst.QUERY_MENU_SUCCESS, menu);
    }

    /**
     * 编辑菜单及关联信息
     * @param roleIds
     * @param menu
     * @return
     */
    @PostMapping("edit")
    public Result edit(Integer[] roleIds,@RequestBody Menu menu) {
        menuService.edit(menu,roleIds);
        return new Result(true, MessageConst.EDIT_MENU_SUCCESS);
    }

    /**
     * 查所有角色
     *
     * @return
     */
    @GetMapping("findAllRole")
    public Result findAllRole() {
        List<Role> roleList = menuService.findAllRole();
        return new Result(true, MessageConst.ACTION_SUCCESS, roleList);
    }

    /**
     * 请求关联的roleid集和
     *
     * @param id
     * @return
     */
    @GetMapping("findRoleIdsByMenuId")
    public Result findRoleIdsByMenuId(Integer id) {
        List<Integer> roleIdList = menuService.findRoleIdsByMenuId(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, roleIdList);
    }

    /**
     * 查询父菜单信息
     * @return
     */
    @GetMapping("findFuMenu")
    public Result findFuMenu() {
        List<Menu> fuMenuList = menuService.findFu();
        log.info("[父菜单] : {}",fuMenuList);
        return new Result(true,MessageConst.QUERY_MENU_SUCCESS,fuMenuList);
    }
    @PostMapping("delete")
    public Result delete(Integer id) {
        log.info("[删除菜单]");
        Menu menu = menuService.findById(id);
        if (menu.getParentMenuId() == null) {
            return new Result(false,MessageConst.DELETE_MENU_FAIL);
        }
        menuService.deleteById(id);
        return new Result(true,MessageConst.DELETE_MENU_SUCCESS);
    }
}
